<?php

namespace app\api\controller\v1;
use think\admin\Controller;
use think\Response;
use app\data\model\DataMember;
use app\data\service\JwtTokenService;
/**
 * 用户数据接口
 * @class User
 * @package app\api\controller\v1
 *
 */
class User extends Controller
{
    public function get(){
        $user = DataMember::mk()->where(['id'=>$this->request->uid])
            ->field('id,nickname,avatar,gender,birthday,signature,mobile')->find();
        $res = $user->getData();
        $this->success('success',$res);
    }
    //修改头像
    function avatar(){
        if(input('post.avatar')!=''){
            $res = DataMember::mk()->where(['id'=>$this->request->uid])->update(['avatar'=> input('post.avatar')]);
            $this->success('修改成功',$res);
        }

    }
    //用户登录
    public function login()
    {
        $type = input('post.type');
        $userModel = new DataMember;
        $user=[];
        $mobile = input('post.mobile');
        if($type == 'code'){
            //验证码登录
            $code = input('post.code');
            if($code!=cache('sms_code_'.$mobile)){
               // $this->error('验证码错误');
            }
            if($code!='123'){
                $this->error('验证码请输入123');
            }
            $user = $userModel::mk()->where(['mobile'=>$mobile])->field('id,mobile,nickname,avatar,signature')->find();
            if(empty($user)){
                $data=['mobile'=>$mobile];
                $uid = $userModel::mk()->insertGetId($data);
                $user = $userModel::mk()->where(['id'=>$uid])->field('id,mobile,nickname,avatar,signature')->find();
            }
        }
        elseif($type == 'pwd'){
            //密码登录
            if(input('post.pwd') == ''){
                $this->error('密码不能为空');
            }
            $pwd  = md5('tata'.input('post.pwd'));
            $user = $userModel::mk()
                ->where(['mobile'=>$mobile,'pwd'=>$pwd])
                ->field('id,mobile,nickname,avatar,signature')->find();
            if(empty($user)){
                $this->error('用户名或密码错误');
            }
        }else{
            $this->error('请选择登录方式');
        }
        $jwt = new JwtTokenService();
        $token = $jwt->sign_token(['id'=>$user['id']]);
        $this->success('登录成功',['token'=>$token,'user'=>$user]);
    }
    //修改密码
    public function change_pwd()
    {
        $data = input('post.');
        if($data['code']!='123'){
            $this->error('验证码请输入123');
        }
        $mobile = DataMember::mk()->where(['id'=>$this->request->uid])->value('mobile');
        if($mobile != $data['mobile']){
            $this->error('手机号码错误');
        }
        if($data['pwd']!=$data['pwd1']){
            $this->error('两次密码不一致');
        }
        DataMember::mk()->where(['id'=>$this->request->uid])->update(['pwd'=>md5('tata'.$data['pwd'])]);
        $this->success('修改密码成功');

    }
    //找回密码
    function find_pwd(){
        $data = input('post.');
        if($data['code']!='123'){
            $this->error('验证码请输入123');
        }
        if($data['pwd']!=$data['pwd1']){
            $this->error('两次密码不一致');
        }
        $userId = DataMember::mk()->where(['mobile'=>$data['mobile']])->value('id');
        if($userId == ''){
            $this->error('手机号码不存在');
        }
        DataMember::mk()->where(['id'=>$userId])->update(['pwd'=>md5('tata'.$data['pwd'])]);
        $this->success('修改密码成功');
    }
    //修改手机号码
    public function changephone(){
        $data = input('post.');
        if($data['code']!='123'){
            $this->error('验证码请输入123');
        }
        $new_mobile = $data['mobile'];
        $check = DataMember::mk()->where(['mobile'=>$new_mobile])->count();
        if($check){
            $this->error('该手机号码已存在');
        }
        $old_mobile = DataMember::mk()->where(['id'=>$this->request->uid])->value('mobile');
        if($new_mobile == $old_mobile){
            $this->error('手机号码未做更改');
        }else{
            DataMember::mk()->where(['id'=>$this->request->uid])->update(['mobile'=>$new_mobile]);
            $redis = redis();
            $token = $this->request->header('token');
            $redis->del($token);
            $this->success('修改手机成功');
        }
    }
    //退出登录
    public function logout(){
        $redis = redis();
        $token = $this->request->header('token');
        $redis->del($token);
        $this->success('退出成功');
    }
    //编辑资料
    public function edit(){
        $params = input('post.');
        $data=[
            'avatar'=>$params['avatar'],
            'gender'=>$params['gender'],
            'birthday'=>$params['birthday'],
            'nickname'=>$params['nickname'],
            'signature'=>$params['signature'],
        ];
        DataMember::mk()->where(['id'=>$this->request->uid])->update($data);
        $this->success('修改成功');
    }
}
